<template>
  <div :class="className" :style="{ height: height, width: width }"/>
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'
import moment from 'moment'
import {indexLine} from "@/api/service/index/analyse";

require('echarts/theme/macarons') // echarts theme
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    // chartData: {
    //   type: Object,
    //   required: true
    // }
  },
  data() {
    return {
      rechargeLine: [],
      regLine: [],
      callLine: [],
      chart: null
    }
  },
  watch: {
    // chartData: {
    //   deep: true,
    //   handler(val) {
    //     this.setOptions(val)
    //   }
    // }
  },
  created() {
    this.getIndexLine()

  },
  mounted() {

  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    findObjectByKeyValue(array, key, value) {
      // 使用 find 方法来查找第一个满足条件的元素
      const found = array.find(item => item[key] === value);
      // 如果找到了匹配项，则返回该对象；否则返回 0
      return found || {
        time: value, sumNum: 0
      };
    },
    getIndexLine() {
      this.loading = true;
      indexLine({}).then(response => {
        this.indexLine = response.data
        let timeList = this.initTime();
        timeList.forEach(item => {
          this.rechargeLine.push(this.findObjectByKeyValue(response.data.rechargeLine, "time", item).sumNum)
          this.regLine.push(this.findObjectByKeyValue(response.data.regLine, "time", item).sumNum)
          this.callLine.push(this.findObjectByKeyValue(response.data.callLine, "time", item).sumNum)
        })
        this.$nextTick(() => {
          this.initChart()
        })

      });
    },
    initTime() {
      // 设置开始时间和结束时间
      const startTime = '00:00';
      const endTime = '23:30';
      // 创建开始和结束的Moment对象
      const startMoment = moment(startTime, 'HH:mm');
      const endMoment = moment(endTime, 'HH:mm');
      // 初始化当前时间为开始时间
      let currentMoment = startMoment.clone();
      // 创建时间列表
      const times = [];
      while (currentMoment.isBefore(endMoment) || currentMoment.isSame(endMoment)) {
        times.push(currentMoment.format('HH:mm')); // 将当前时间格式化并添加到列表中
        currentMoment.add(30, 'minutes'); // 增加30分钟
      }
      return times;
    },

    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    setOptions() {
      this.chart.setOption({
        toolbox: {
          feature: {
            magicType: {
              type: ['line', 'bar']
            }
          }
        },
        xAxis: {
          data: this.initTime(),
          boundaryGap: false,
          axisTick: {
            show: false
          }
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisTick: {
            show: false
          }
        },
        legend: {
          data: ['充值次数', '通话次数', '注册用户']
        },
        series: [
          {
            name: '充值次数', itemStyle: {
              normal: {
                color: '#FF005A',
                lineStyle: {
                  color: '#FF005A',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: this.rechargeLine,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '通话次数', itemStyle: {
              normal: {
                color: '#17ee78',
                lineStyle: {
                  color: '#17ee78',
                  width: 2
                }
              }
            },
            smooth: true,
            type: 'line',
            data: this.callLine,
            animationDuration: 2800,
            animationEasing: 'cubicInOut'
          },
          {
            name: '注册用户',
            smooth: true,
            type: 'line',
            itemStyle: {
              normal: {
                color: '#3888fa',
                lineStyle: {
                  color: '#3888fa',
                  width: 2
                },
                areaStyle: {
                  color: '#f3f8ff'
                }
              }
            },
            data: this.regLine,
            animationDuration: 2800,
            animationEasing: 'quadraticOut'
          }]
      })
    }
  }
}
</script>
